package org.obs.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name = "BLOG")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Blog implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@OneToOne(mappedBy = "blog", fetch = FetchType.LAZY)
	private User owner;
	
	@OneToMany(fetch=FetchType.LAZY, mappedBy="blog", cascade=CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.EXTRA) 
	private Set<BlogEntry> entries;
	
	public void addEntry(BlogEntry entry) {
		entry.setBlog(this);
		getEntries().add(entry);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public Set<BlogEntry> getEntries() {
		if (entries == null) {
			entries = new HashSet<BlogEntry>();
		}
		return entries;
	}

	public void setEntries(Set<BlogEntry> entries) {
		this.entries = entries;
	}

}
